﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 林业信息工程
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {
            string filename = "lyxxgc.mdb";
            string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename;
            OleDbConnection oc = new OleDbConnection();
            oc.ConnectionString = str;
            oc.Open();
            OleDbCommand ocm = new OleDbCommand();
            string str1 = "select * from 表1";
            ocm.CommandText = str1;
            ocm.Connection = oc;
            OleDbDataReader myread = ocm.ExecuteReader();
            List<string> name = new List<string>();
            List<string> biename = new List<string>();
            while (myread.Read())
            {
                name.Add((string)myread[1]);
                biename.Add((string)myread[2]);
            }

            //textbox模糊查找
            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
            foreach (string l in name)
            {
                collection.Add(l);
            }
            foreach (string l2 in biename)
            {
                collection.Add(l2);
            }
            textBox2.AutoCompleteCustomSource = collection;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filename = "lyxxgc.mdb";
            string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename;
            OleDbConnection oc = new OleDbConnection();
            oc.ConnectionString = str;
            oc.Open();
            OleDbCommand ocm = new OleDbCommand();
            string str1 = "select * from 表1 where 花卉名称 =" + "'" + textBox2.Text + "'";
            ocm.CommandText = str1;
            ocm.Connection = oc;
            OleDbDataReader myread = ocm.ExecuteReader();
            if (myread.Read())
            {
                textBox1.Text = "花卉名称:" + (string)myread[1] + "\r\n";
                textBox1.Text += "花卉别名:" + (string)myread[2] + "\r\n";
                textBox1.Text += "科中文名:" + (string)myread[3] + "\r\n";
                textBox1.Text += "属中午名:" + (string)myread[4] + "\r\n";
                textBox1.Text += "花卉类型:" + (string)myread[5] + "\r\n";
                textBox1.Text += "\r\n";
                textBox1.Text += "形态特性:" + (string)myread[6] + "\r\n";
                textBox1.Text += "\r\n";
                textBox1.Text += "分布习性:" + (string)myread[7] + "\r\n";
                textBox1.Text += "\r\n";
                textBox1.Text += "生物特性:" + (string)myread[8] + "\r\n";
                textBox1.Text += "\r\n";
                textBox1.Text += "园林用途:" + (string)myread[9] + "\r\n";
                string url = (string)myread[10];
                System.Net.WebRequest webreq = System.Net.WebRequest.Create(url);
                System.Net.WebResponse webres = webreq.GetResponse();
                using (System.IO.Stream stream = webres.GetResponseStream())
                {
                    pictureBox2.Image = Image.FromStream(stream);

                }
            }
            else
            {
                MessageBox.Show("无查询结果");
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string filename = "lyxxgc.mdb";
            string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename;
            OleDbConnection oc = new OleDbConnection();
            oc.ConnectionString = str;
            oc.Open();
            OleDbCommand ocm = new OleDbCommand();
            string str1 = "select * from 表1 where 花卉名称 =" + "'" + textBox2.Text + "'";
            ocm.CommandText = str1;
            ocm.Connection = oc;
            OleDbDataReader myread = ocm.ExecuteReader();
            string text = null;
            if (myread.Read())
            {
                text = "花卉名称:" + (string)myread[1] + "\r\n";
                text += "花卉别名:" + (string)myread[2] + "\r\n";
                text += "科中文名:" + (string)myread[3] + "\r\n";
                text += "属中午名:" + (string)myread[4] + "\r\n";
                text += "花卉类型:" + (string)myread[5] + "\r\n";
            }
            pictureBox1.Image = Encoder.code(text);
            pictureBox1.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.RestoreDirectory = true;                                 //打开上次路径
            save.Filter = "(*.png)|*.png|(*.bmp)|*.bmp";                  //过滤格式
            if (save.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image.Save(save.FileName);                    //保存
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
